/*
 * Created on Oct 15, 2004
 *
 */
package com.erclab.internal.xpresso.formelements;

import com.erclab.internal.xpresso.conf.Settings;
import com.erclab.internal.xpresso.dataAccess.ApplicationDataSource;
import com.erclab.internal.xpresso.dataAccess.GenericResultSet;
import com.erclab.internal.xpresso.dataAccess.QueryBuilder;
import com.erclab.internal.xpresso.datavalidators.FormElementValidator;
import com.erclab.internal.xpresso.exceptions.DataSourceException;
import com.erclab.internal.xpresso.exceptions.FormException;
import com.erclab.internal.xpresso.forms.Form;

/**
 * @author rodrigen
 *
 */
public abstract class FormElement {

	protected int idFormElement;
	protected int displayOrder;
	protected String destinationTable;
	protected String destinationColumn;
	protected int destinationDataSourceId;
	protected boolean required;
	protected String label;
	protected String description;
	protected String dataType;
	protected String classHandler;
	protected String style;
	protected String lookupQuery;
	protected int lookupDataSourceId;
	protected String parameters;
	protected String originalValue;
	protected Object userValue;
	protected FormElementValidator dataValidator;
	protected String errorMessage;

	public abstract String toHTML(Form theForm);
	public abstract void loadLookupValues(Form theForm) throws FormException;
	public abstract boolean isModifiable();
	
	/**
	 * @return
	 */
	public int getIdFormElement() {
		return idFormElement;
	}

	/**
	 * @return
	 */
	public int getDisplayOrder() {
		return displayOrder;
	}

	
	/**
	 * @return
	 */
	public String getDestinationTable() {
		return destinationTable;
	}

	/**
	 * @return
	 */
	public String getDestinationColumn() {
		return destinationColumn;
	}

	/**
	 * @return
	 */
	public boolean getRequired() {
		return required;
	}

	/**
	 * @return
	 */
	public java.lang.String getLabel() {
		return label;
	}

	/**
	 * @return
	 */
	public java.lang.String getDescription() {
		return description;
	}

	/**
	 * @return
	 */
	public java.lang.String getDataType() {
		return dataType;
	}

	/**
	 * @param string
	 */
	public void setDataType(java.lang.String string) {
		dataType = string;
	}

	/**
	 * @param string
	 */
	public void setDescription(java.lang.String string) {
		description = string;
	}

	/**
	 * @param string
	 */
	public void setDestinationColumn(String string) {
		destinationColumn = string;
	}

	/**
	 * @param string
	 */
	public void setDestinationTable(String string) {
		destinationTable = string;
	}

	/**
	 * @param i
	 */
	public void setDisplayOrder(int i) {
		displayOrder = i;
	}

	/**
	 * @param i
	 */
	public void setIdFormElement(int i) {
		idFormElement = i;
	}

	/**
	 * @param string
	 */
	public void setLabel(java.lang.String string) {
		label = string;
	}

	/**
	 * @param b
	 */
	public void setRequired(boolean b) {
		required = b;
	}

	/**
	 * @return
	 */
	public java.lang.String getClassHandler() {
		return classHandler;
	}

	/**
	 * @param string
	 */
	public void setClassHandler(java.lang.String string) {
		classHandler = string;
	}

	/**
	 * @return
	 */
	public java.lang.String getStyle() {
		return style;
	}

	/**
	 * @param string
	 */
	public void setStyle(java.lang.String string) {
		style = string;
	}

	/**
	 * @return
	 */
	public String getLookupQuery() {
		return lookupQuery;
	}

	/**
	 * @param lookup
	 */
	public void setLookupQuery(String lookupQuery) {
		this.lookupQuery = lookupQuery;
	}

	/**
	 * @return
	 */
	public String getParameters() {
		return parameters;
	}

	/**
	 * @param string
	 */
	public void setParameters(String string) {
		parameters = string;
	}

	/**
	 * @return
	 */
	public int getLookupDataSourceId() {
		return lookupDataSourceId;
	}

	/**
	 * @param i
	 */
	public void setLookupDataSourceId(int i) {
		lookupDataSourceId = i;
	}


	/**
	 * @param string
	 */
	public void setDestinationDataSourceId(int i) {
		destinationDataSourceId = i;
	}
	
	/**
	 * @return
	 */
	public int getDestinationDataSourceId() {
		return destinationDataSourceId;
	}

	/**
	 * @return
	 */
	public String getOriginalValue() {
		return originalValue;
	}
	
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("IdFormElement: " + this.idFormElement + "\n");
		
		return sb.toString();
	}

	/**
	 * 
	 */
	public void loadOriginalValue(Form f) throws FormException {
		ApplicationDataSource ads = Settings.getRepository().getApplicationDataSource(this.destinationDataSourceId);
		if (ads == null){
			throw new FormException("The data source specified by the element does not exist. FE ID: "+ this.idFormElement);	
		}
		
		QueryBuilder qb = ads.getQueryBuilder();
		String query = qb.createFormElementQuery(f, this, f.getFormParameters());
		GenericResultSet grs = null;
		try {
			grs = ads.runQuery(query);
		} catch (DataSourceException e) {
			throw new FormException("An error ocurred loading form element original value: " + e);
		}
		
		if(grs.nextRow() && grs.getRow() != null){
			this.originalValue = grs.getRow()[0];		
		} else{
			// TODO: what to do when the original value is not found ??
			//throw new FormException("The original value could not be found. FormElement: " + this);
		}
		
	}


	public String[] processSubmittedValues(Form theForm, String[] submittedValues){
		return submittedValues;
	}

	public boolean isMultipleValue(){
		return false;
	}

	public boolean isHidden(){
		return false;
	}
	
	public Object getUserValue() {
		return userValue;
	}
	public void setUserValue(Object userValue) {
		this.userValue = userValue;
	}
	
	public FormElementValidator getDataValidator() {
		return dataValidator;
	}
	
	public void setDataValidator(FormElementValidator dataValidator) {
		this.dataValidator = dataValidator;
	}
	public String getErrorMessage() {
		return errorMessage;
	}
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

}
